package com.example.gybicycle.common.config;


import com.example.gybicycle.common.entity.Result;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

@ControllerAdvice
public class MyExceptionHandler {
    @ExceptionHandler(value =Exception.class)
    @ResponseBody
    public Result exceptionHandler(Exception e){
        System.out.println("全局异常捕获>>>:"+e.getMessage());
        String msg=e.getMessage();
        if(!msg.contains("No static resource favicon.ico"))
            e.printStackTrace();
        if(msg.contains("Duplicate entry"))
           return Result.oneKey("当前数据已存在");
        else if(msg.contains("Subject does not have permission"))
            return  Result.noPower();
        else if(msg.contains("null"))
            return  Result.error("空指针错误"+msg);
        else if(msg.contains("This subject is anonymous"))
            return  Result.error("权限配置错误");
        else if (msg.contains("No static resource")) {
            return  Result.error("当前接口不存在");
        } else if (msg.contains("JSON parse error")) {
            return  Result.error("JSON 格式错误"+msg);
        } else
            return  Result.error("网络错误",msg);

    }

}
